package com.zxb.base.task;

import com.zxb.base.task.service.QuartzServiceImpl;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author Zxb
 */
@Component
public class AppListener implements ApplicationListener<ApplicationEvent>{

	private static Boolean loaded = false;

    private QuartzServiceImpl quartzService;

    public AppListener(QuartzServiceImpl quartzService) {
    	this.quartzService = quartzService;
    }
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if(event instanceof ContextRefreshedEvent){
			if(!loaded){
				loaded = true;
				try {
					quartzService.startAll();
		        } catch (SchedulerException e) {
		            e.printStackTrace();
		        }
			}
		}

	}

}
